//
//  fprops.swift
//  xzbuyer
//
//  Created by zhuxietong on 2021/2/9.
//

import Foundation
import Eelay
import RxSwift

extension FForm{
    open class Format {
        open var tag:String{
            return "FForm.FForm"
        }
        public var valueObject:Any? = nil
        open var displayString:String{
            return "\(valueObject ?? "")"
        }
        
        open var formValue:Any{
            return valueObject ?? ""
        }
        
        public static let `default`:Format = Format()
        public init() {
            
        }
        
    }
    
    public enum TimePickerMode{
        case year
        case month
        case day
        case hour
        case minute
        case second
        
    }
    public enum TimeFormatType {
        
        
        case millisecond_stamp
        case second_stamp
        case dispay(String)//YYYY-MM-dd
        public var key:String{
            switch self {
            case .dispay(let display):
                return display
            default:
                return "STAMP"
            }
        }
    }


}



public enum FProps {
    public var priority:CGFloat{
        switch self {
        case .emptyTip,.errorTip:
            return 1001
        case .label,.placeholder:
            return 1000
        case .name:
            return 800
        default:
            return 1000
        }
    }
    
    case name(String,String)
    case value(Any)
    case label(String)
    case verify(FVerify)
    case placeholder(String)
    case options([[String:Any]])
    case required(Bool)
    case visible(Bool)
    case emptyTip(String)
    case errorTip(String)
    case type(InputType)
    case count(min:Int,max:Int)
    case child(FField)
    case model([String:Any])
    case num_limit(NumLimit)
    case format(FForm.Format)
    public var key:String{
        get{
            switch self {
            case .format:
                return "format"
            case .required:
                return "required"
            case .placeholder:
                return "placeholder"
            case .name:
                return "name"
            case .child:
                return "child"
            case .count:
                return "count"
            case .emptyTip:
                return "emptyTip"
            case .errorTip:
                return "errorTip"
            case .label:
                return "lable"
            case .model:
                return "model"
            case .verify:
                return "verify"
            case .visible:
                return "visible"
            case .value:
                return "value"
            case .options:
                return "options"
            case .type:
                return "type"
            case .num_limit:
                return "num_limit"

            }
        }
    }

}

extension Array where Self.Element == FProps {
    public mutating func update(_ prop:FProps){
        var index:Int? = nil
        for (i,p) in self.enumerated() {
            if p.key == prop.key{
                index = i
            }
        }
        if let i = index{
            remove(at: i)
        }
        
        self.append(prop)
    }
    
    public var emptyTip:String{
        let ks = self.sorted{$0.priority > $1.priority}
        for item in ks {
            switch item {
            case .emptyTip(let msg):
                return msg
            default:
                break
            }
        }
        return "检查\(label)"
    }
    public var errorTip:String{
        let ks = self.sorted{$0.priority > $1.priority}
        for item in ks {
            switch item {
            case .errorTip(let msg):
                return msg
            default:
                break
            }
        }
        return "检查\(label)"
    }
    
    
    public var type:InputType{
        get{
            for p in self {
                switch p {
                case .type(let type):
                    return type
                default:
                    break
                }
            }
            return .input
        }
    }
    public var format:FForm.Format{
        get{
            for p in self {
                switch p {
                case .format(let format):
                    return format
                default:
                    break
                }
            }
            return FForm.Format.default
        }
    }
    
    public var num_limit:NumLimit{
        get{
            for p in self {
                switch p {
                case .num_limit(let limit):
                    return limit
                default:
                    break
                }
            }
            return .init(min: 1, minMask: "请\(type.name)\(label)", max: 10000, maxMask: "")
//            return .init(min: 1, minMask: "不能少于", max: 100000, maxMask: "不能大于")
        }
    }
    
    public var options:[[String:Any]]{
        get{
            for p in self {
                switch p {
                case .options(let ops):
                    return ops
                default:
                    break
                }
            }
            return []
        }
    }
    
    public var name:String{
        get{
            for p in self {
                switch p {
                case .name(let name,_):
                    return name
                default:
                    break
                }
            }
            return ""
        }
    }
    public var label:String{
        get{
            let ks = self.sorted{$0.priority > $1.priority}
            for p in ks {
                switch p {
                case .label(let name):
                    return name
                default:
                    break
                }
            }
            for p in self {
                switch p {
                case .name(_,let title):
                    return title
                default:
                    break
                }
            }
            return ""
        }
    }
    
    public var required:Bool{
        get{
            for p in self {
                switch p {
                case .required(let r):
                    return r
                default:
                    break
                }
            }
            return true
        }
    }

}


